Telegram Group & Telegram Channel
🧠 Хитрая задача на C++: "Исчезающее число среди строк"

Условие
Дан список строк, представляющих числа от 1 до 100 включительно. Одного числа нет.
Твоя задача — найти, какого именно числа не хватает.
Нельзя использовать сортировку, std::accumulate, std::unordered_map, std::stoi можно.

Пример:
Массив: ["1", "2", "3", ..., "98", "99"] (без "100")
Ответ: 100

Формат:


int findMissingNumber(const std::vector<std::string>& data);


Решение с XOR


#include <iostream>
#include <vector>
#include <string>

int findMissingNumber(const std::vector<std::string>& data) {
int xor_full = 0;
int xor_data = 0;

for (int i = 1; i <= 100; ++i) {
xor_full ^= i;
}

for (const auto& s : data) {
xor_data ^= std::stoi(s);
}

return xor_full ^ xor_data;
}


Пример использования:


int main() {
std::vector<std::string> data;
for (int i = 1; i <= 100; ++i) {
if (i != 57) { // Удалим 57
data.push_back(std::to_string(i));
}
}

std::cout << "Пропущено: " << findMissingNumber(data) << std::endl;
return 0;
}


Почему работает:
XOR — идеальное решение, когда нужно найти одну потерянную величину среди уникальных значений.
a ^ a = 0, 0 ^ b = b. Поэтому:
(XOR всех от 1 до 100) ^ (XOR из данных) = отсутствующее число.

@cpluspluc



tg-me.com/cpluspluc/1083
Create:
Last Update:

🧠 Хитрая задача на C++: "Исчезающее число среди строк"

Условие
Дан список строк, представляющих числа от 1 до 100 включительно. Одного числа нет.
Твоя задача — найти, какого именно числа не хватает.
Нельзя использовать сортировку, std::accumulate, std::unordered_map, std::stoi можно.

Пример:
Массив: ["1", "2", "3", ..., "98", "99"] (без "100")
Ответ: 100

Формат:


int findMissingNumber(const std::vector<std::string>& data);


Решение с XOR


#include <iostream>
#include <vector>
#include <string>

int findMissingNumber(const std::vector<std::string>& data) {
int xor_full = 0;
int xor_data = 0;

for (int i = 1; i <= 100; ++i) {
xor_full ^= i;
}

for (const auto& s : data) {
xor_data ^= std::stoi(s);
}

return xor_full ^ xor_data;
}


Пример использования:


int main() {
std::vector<std::string> data;
for (int i = 1; i <= 100; ++i) {
if (i != 57) { // Удалим 57
data.push_back(std::to_string(i));
}
}

std::cout << "Пропущено: " << findMissingNumber(data) << std::endl;
return 0;
}


Почему работает:
XOR — идеальное решение, когда нужно найти одну потерянную величину среди уникальных значений.
a ^ a = 0, 0 ^ b = b. Поэтому:
(XOR всех от 1 до 100) ^ (XOR из данных) = отсутствующее число.

@cpluspluc

BY C++ Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpluspluc/1083

View MORE
Open in Telegram


C Academy Telegram | DID YOU KNOW?

Date: |

How to Invest in Bitcoin?

Like a stock, you can buy and hold Bitcoin as an investment. You can even now do so in special retirement accounts called Bitcoin IRAs. No matter where you choose to hold your Bitcoin, people’s philosophies on how to invest it vary: Some buy and hold long term, some buy and aim to sell after a price rally, and others bet on its price decreasing. Bitcoin’s price over time has experienced big price swings, going as low as $5,165 and as high as $28,990 in 2020 alone. “I think in some places, people might be using Bitcoin to pay for things, but the truth is that it’s an asset that looks like it’s going to be increasing in value relatively quickly for some time,” Marquez says. “So why would you sell something that’s going to be worth so much more next year than it is today? The majority of people that hold it are long-term investors.”

In many cases, the content resembled that of the marketplaces found on the dark web, a group of hidden websites that are popular among hackers and accessed using specific anonymising software.“We have recently been witnessing a 100 per cent-plus rise in Telegram usage by cybercriminals,” said Tal Samra, cyber threat analyst at Cyberint.The rise in nefarious activity comes as users flocked to the encrypted chat app earlier this year after changes to the privacy policy of Facebook-owned rival WhatsApp prompted many to seek out alternatives.C Academy from sg


Telegram C++ Academy
FROM USA